package es.ats.codington.festival.services;

import java.sql.Connection;
import java.util.ArrayList;
import es.ats.codington.festival.daos.EventDAOImpl;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.interfaces.services.IEventService;

/**
 * The Class EventServiceImpl.
 */
public class EventServiceImpl implements IEventService{
		
	/**
	 * Insert event.
	 *
	 * @param insertevent the insertevent
	 * @return the int
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.services.IEventService#insertEvent(es.ats.codington.festival.domain.Event)
	 */
  	public int insertEvent(Event insertevent) throws Exception{
		  Connection con = null; 
		  int rows = 0;
		  
		  //Create instance of ServiceManager to Connect with DB 
		  ServiceManager serviceManager = new ServiceManager();
		  
		  //Create instance of EventDAO
		  EventDAOImpl eventDAO = new EventDAOImpl();
		  
		  try{
			  // OpenConnection
			  con = serviceManager.OpenConnectionToDB();
			  
			  // Insert Event
			  rows = eventDAO.insertEventDAO(con, insertevent);
			  
			  //CloseConnection
			  serviceManager.CloseConnectionToDB(con);
			  
		  }catch(Exception ex){
				System.out.println(ex.getMessage());
		  }
		  
		  return rows;
  
	  }
	  
	/**
	 * Delete event.
	 *
	 * @param idEvent the id event
	 * @return the int
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.services.IEventService#deleteEvent(int)
	 */
  	public int deleteEvent(int idEvent)throws Exception{
		  Connection con = null; 
		  int rows = 0;
		  
		  //Create instance of ServiceManager to Connect with DB 
		  ServiceManager serviceManager = new ServiceManager();
		  
		  //Create instance of EventDAO
		  EventDAOImpl eventDAO = new EventDAOImpl();
		  
		  try{
			  // OpenConnection
			  con = serviceManager.OpenConnectionToDB();
			  
			  // Insert Event
			  rows = eventDAO.deleteEventDAO(con, idEvent);
			  
			  //CloseConnection
			  serviceManager.CloseConnectionToDB(con);
			  
		  }catch(Exception ex){
				System.out.println(ex.getMessage());
		  }
		  
		  return rows;
	  }
	  
	/**
	 * Update event.
	 *
	 * @param updateEvent the update event
	 * @return the int
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.services.IEventService#updateEvent(es.ats.codington.festival.domain.Event)
	 */
  	public int updateEvent(Event updateEvent)throws Exception{
		  Connection con = null; 
		  int rows=0;
		  
		  //Create instance of ServiceManager to Connect with DB 
		  ServiceManager serviceManager = new ServiceManager();
		  
		  //Create instance of EventDAO
		  EventDAOImpl eventDAO = new EventDAOImpl();
		  
		  try{
			  // OpenConnection
			  con = serviceManager.OpenConnectionToDB();
			  
			  // Insert Event
			  rows = eventDAO.updateEventDAO(con, updateEvent);
			  
			  //CloseConnection
			  serviceManager.CloseConnectionToDB(con);
			  
		  }catch(Exception ex){
				System.out.println(ex.getMessage());
		  }
		  
		  return rows;
	  }
	  
	  
	/**
  	 * Obtain all event.
  	 *
  	 * @return the array list
  	 */
  	public ArrayList<Event> obtainAllEvent() {
			Connection con = null;
			ServiceManager serviceManager = new ServiceManager();
			EventDAOImpl eventDAO = new EventDAOImpl();

			ArrayList<Event> eve = new ArrayList<Event>();
			try{
				con = serviceManager.OpenConnectionToDB();			
				eve = eventDAO.obtainAllEventDAO(con);
				serviceManager.CloseConnectionToDB(con);
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			return eve;
		}
	  
	/**
	 * Search eventby id.
	 *
	 * @param idEvent the id event
	 * @return the event
	 * @see es.ats.codington.festival.interfaces.services.IEventService#searchEventbyID(int)
	 */
  	public Event searchEventbyID(int idEvent){
		  Connection con = null;
		  
		  ServiceManager serviceManager = new ServiceManager();
		
		  EventDAOImpl eventDAO = new EventDAOImpl();

		  Event eve= new Event();

			try{
				con = serviceManager.OpenConnectionToDB();			
				eve = eventDAO.searchEventbyIdDAO(idEvent, con);
				serviceManager.CloseConnectionToDB(con);
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			
			return eve;
	  }
	  
		/**
		 * Show event.
		 *
		 * @param event the event
		 * @see es.ats.codington.festival.interfaces.services.IEventService#ShowEvent(es.ats.codington.festival.domain.Event)
		 */
		public void ShowEvent(Event event){
		/*	System.out.println("Data of Event: ");
			System.out.println("Name: "+event.getName());
			System.out.println("Description: "+event.getDescription());
			System.out.println("Place: "+event.getPlace().getPlaceName());
			System.out.println("Type Event: "+event.getTypeEvent());	*/
			
		}
}
